package cn.chainfind.web.mapper;


import cn.chainfind.web.domain.CompanyResult;
import cn.chainfind.web.domain.EasyLoanCompany;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.util.List;

@SpringBootTest
public class DataUtil {
    @Autowired
    private EasyLoanCompanyMapper companyMapper;

    @Test
    public void testSelect() {
//        System.out.println(("----- selectAll method test ------"));
//        File file = new File("/Users/zcb/Work/IdeaProjects/WorkDay/easy_lean.json");
//        JSON dataJson = JSONUtil.readJSON(file, CharsetUtil.CHARSET_UTF_8);
//        System.out.println(dataJson);
//
//        CompanyResult companyResult = dataJson.toBean(CompanyResult.class);
//        List<EasyLoanCompany> reCORDS = companyResult.getData().getRecords();
//        companyMapper.insertOrUpdate(reCORDS);



        File[] files = FileUtil.ls("/Users/zcb/Work/IdeaProjects/WorkDay/output");
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if(file.getName().contains(".DS_Store")) continue;
//            System.out.println(file.getName());
            JSON dataJson = JSONUtil.readJSON(file, CharsetUtil.CHARSET_UTF_8);

            CompanyResult companyResult = dataJson.toBean(CompanyResult.class);
            List<EasyLoanCompany> reCORDS = companyResult.getData().getRecords();
            companyMapper.insertOrUpdate(reCORDS);
        }
        System.out.println("=============数据入库完成======");



//        int dataRowNum=-1;
//        int startPage = 1;
//        int pageSize = 100;
//        do {
//            List<EasyLoanCompany> tmpData = EasyLeanUtil.collectData(startPage,pageSize);
//            dataRowNum = tmpData.size();
//            companyMapper.insert(tmpData);
//
//            startPage++;
//
//        }while (dataRowNum>=pageSize);
//        System.out.println("=============数据入库完成======");

    }

}
